﻿using System;
using System.Collections.Generic;
using System.Text;

namespace GReporting.Core.PagingAlgorithm
{
    public class SummaryBuilder : AbstractPageBuilder
    {
        public override void Build(PageBuilderContext context)
        {
            context.CurrentBand = context.Report.Layout.summary;
            if (context.Report.Layout.isSummaryNewPage)
            {
                if (context.IsLastPage)
                {
                    if (isPrint(context.CurrentBand, context.Engine))
                    {
                        int printBandHeight = PrintBand(context);
                        context.Y += printBandHeight;
                        context.PrintHeight += printBandHeight;
                    }
                    AbstractPageBuilder builder = new LastPageFooterBuilder();
                    builder.Build(context);
                }
                else
                {
                    if (context.DataRowIndex >= context.SourceTable.Rows.Count&&context.UnfinishedGroupFooter==false)
                    {
                        context.IsLastPage = true;
                        AbstractPageBuilder builder = new PageFooterBuilder();
                        builder.Build(context);
                        context.NextPageSet();
                        builder = new TitleBuilder();
                        builder.Build(context);
                    }
                    else
                    {
                        AbstractPageBuilder builder = new PageFooterBuilder();
                        builder.Build(context);
                    }
                }
            }
            else
            {
                if (context.DataRowIndex >= context.SourceTable.Rows.Count && context.UnfinishedGroupFooter == false)
                {
                    context.IsLastPage = true;
                    if (isPrint(context.CurrentBand, context.Engine))
                    {
                        int printBandHeight = PrintBand(context);
                        context.Y += printBandHeight;
                        context.PrintHeight += printBandHeight;
                    }
                    AbstractPageBuilder builder = new LastPageFooterBuilder();
                    builder.Build(context);
                }
                else
                {
                    if (isPrint(context.CurrentBand, context.Engine))
                    {
                        int printBandHeight = PrintBand(context);
                        context.Y += printBandHeight;
                        context.PrintHeight += printBandHeight;
                    }
                    AbstractPageBuilder builder = new PageFooterBuilder();
                    builder.Build(context);
                }
            }


        }
    }
}
